package com.doublefish.doublefishrobot.ui.video;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.video.adapter.VideoAdapter;
import com.doublefish.doublefishrobot.ui.video.bean.VideoInfoBean;
import com.doublefish.doublefishrobot.ui.video.utils.VideoManager;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;
import com.doublefish.doublefishrobot.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:dugaofeng on 2024/10/15 17:15
 */
public class VideoFragment extends Fragment {

    private View view;

    private View backLL;
    private TextView titleTv;
    private TextView rightTv;
    private RecyclerView recyclerView;

    private List<VideoInfoBean> videoList = new ArrayList<>();
    private VideoAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_video, container, false);

            initViews();
        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (videoList.size() == 0 && AppCacheUtil.getInstance().isStoragePermissionEnable()) {
            //初始化视频管理工具
            VideoManager.getInstance().init(AppCacheUtil.getInstance().getAppContext(), new VideoManager.OnDatabaseInitListner() {
                @Override
                public void onCallback(boolean suc) {
                    if (suc) {
                        getVideoList();
                    } else {
                        ToastUtil.showToast(getActivity(), getString(R.string.error_video_info));
                    }
                }
            });
        }
    }

    private void initViews() {
        backLL = view.findViewById(R.id.ll_back);
        titleTv = view.findViewById(R.id.tv_title);
        rightTv = view.findViewById(R.id.tv_right);
        recyclerView = view.findViewById(R.id.recyclerView);

        backLL.setVisibility(View.INVISIBLE);
        titleTv.setText(R.string.video);

        ViewGroup.LayoutParams layoutParams = rightTv.getLayoutParams();
        layoutParams.width = dip2px(40);
        rightTv.setLayoutParams(layoutParams);
        rightTv.setBackground(ResourcesCompat.getDrawable(getResources(), R.mipmap.setting_2, null));
        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), VideoSettingMenuActivity.class);
                startActivity(intent);
            }
        });

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),2);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new VideoAdapter(getActivity(), videoList);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                VideoInfoBean videoInfoBean = videoList.get(position);
                toPlayVideo(videoInfoBean);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    public void getVideoList() {
        List<VideoInfoBean> list = VideoManager.getInstance().getVideoInfoBeanListIgnoreHD();
        if (list != null) {
            videoList.clear();
            videoList.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    private void toPlayVideo(VideoInfoBean videoInfoBean) {
        Intent intent = VideoPlayerActivity.createIntent(getActivity(), videoInfoBean.getName(), videoInfoBean.getCode());
        startActivity(intent);
    }

    private int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
